//
//  BaseTabbarController.m
//  项目基本框架
//
//  Created by huahua on 17/1/14.
//  Copyright © 2017年 os1. All rights reserved.
//

#import "BaseTabbarController.h"
#import "BaseNavigationController.h"
#import "BaseNormolNavController.h"
#import "PublishTabBar.h"
#import "TabBarButton.h"

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "MiddleViewController.h"  //中间圆形的按钮
#import "LastViewController.h"

@interface BaseTabbarController ()
{
    NSArray *imageNames;
}
@end

@implementation BaseTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.backgroundColor = [UIColor orangeColor];
    
    [self _setupArray];
    
    [self _createNormolTabbar];
    
//    [self _createRoundTabbar];
}

/** 初始化图片数组*/
- (void)_setupArray{
    
    imageNames = @[@"tabBar_essence_icon",@"tabBar_new_icon",@"tabBar_friendTrends_icon"];
}

/** 普通的tabbar*/
- (void)_createNormolTabbar{
    
    // 添加子控制器
    [self setupNormalChildVc:[[FirstViewController alloc] init] title:@"首页"];
    
    [self setupNormalChildVc:[[SecondViewController alloc] init] title:@"中间页"];
    
    [self setupNormalChildVc:[[LastViewController alloc] init] title:@"尾页"];
}

/**
 * 初始化子控制器
 */
- (void)setupNormalChildVc:(UIViewController *)vc title:(NSString *)title {
    // 设置文字和图片
    vc.title = title;
    
    // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器
    BaseNormolNavController *nav = [[BaseNormolNavController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
    for (UIView *view in self.tabBar.subviews) {
        
        if ([view isMemberOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [view removeFromSuperview];
        }
        if ([view isMemberOfClass:[UIImageView class]]) {
            
            [view removeFromSuperview];
        }
        if ([view isMemberOfClass:[TabBarButton class]]) {
            
            [view removeFromSuperview];
        }
    }
    
    float width = self.view.bounds.size.width/self.viewControllers.count;
    for (int index = 0; index<self.viewControllers.count; index++) {
        
        UIViewController *viewController = [self.viewControllers objectAtIndex:index];//取到当前的视图控制器
        TabBarButton *button = [[TabBarButton alloc] initWithFrame:CGRectMake(width*index, 0, width, 49)];
        button.tag = index;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.imageName = imageNames[index];
        button.titleName = viewController.title;
        [self.tabBar addSubview:button];
    }
}

- (void)buttonAction:(TabBarButton *)button {
    
    self.selectedIndex = button.tag;
    NSLog(@"点击了第%ld个",button.tag);
}

/** 中间带有圆弧凸起的tabbar*/
- (void)_createRoundTabbar{
    
    // 添加子控制器
    [self setupChildVc:[[FirstViewController alloc] init] title:@"首页" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    
    [self setupChildVc:[[LastViewController alloc] init] title:@"尾页" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    
    // 更换tabBar
    [self setValue:[[PublishTabBar alloc] init] forKeyPath:@"tabBar"];
}

/**
 * 初始化子控制器
 */
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置文字和图片
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

@end
